<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml">
<!-- 	xmlns:fo="http://www.w3.org/1999/XSL/Format" -->

<xsl:output
	method="xml"
	indent="yes"
	encoding="UTF-8"
	omit-xml-declaration="no"
	doctype-system="about:legacy-compat"/>


<xsl:variable name="page" select="/sun-wars/page/@name"/>
<xsl:variable name="names" select="document('names.xml')/names"/>


<xsl:template name="xparse-tag">
	<xsl:param name="root"/>

	<xsl:choose>
		<xsl:when test="name($root)">
			<xsl:for-each select="$root">
				<xsl:element name="{name()}">
					<xsl:for-each select="@*">
						<xsl:attribute name="{name()}">
							<xsl:value-of select="."/>
						</xsl:attribute>
					</xsl:for-each>
					<xsl:for-each select="node()">
						<xsl:call-template name="xparse-tag">
							<xsl:with-param name="root" select="."/>
						</xsl:call-template>
					</xsl:for-each>
				</xsl:element>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="."/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>


<xsl:template name="hidden-request-keep">
	<xsl:for-each select="/sun-wars/page/request-keep/value">
		<input type="hidden" name="{@name}" value="{text()}" />
	</xsl:for-each>
</xsl:template>

<xsl:template name="str-request-keep"><xsl:for-each select="/sun-wars/page/request-keep/value">&#38;<xsl:value-of select="@name"/>=<xsl:value-of select="text()"/></xsl:for-each></xsl:template>


<!-- norme de duration de xs:duration (PnYnMnDTnHnMnS) :
http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#duration
The lexical representation for duration is the [ISO 8601] extended format PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.
Ne prend pas en comtpe les mois et années et les secondes sont toujours présente-->
<xsl:template name="duration-format">
	<xsl:param name="duration"/>

	<xsl:variable name="day" select="substring-before($duration, 'D')"/>
	<xsl:variable name="hour" select="substring-before($duration, 'H')"/>
	<xsl:variable name="min" select="substring-before($duration, 'M')"/>
	<xsl:variable name="sec" select="substring($duration, string-length($duration) - 2, 2)"/>

	<xsl:variable name="datetime">
		<xsl:if test="$day"><xsl:value-of select="number(substring-after($day, 'P'))"/>jours </xsl:if>
		<xsl:if test="$hour"><xsl:value-of select="number(substring-after($hour, 'T'))"/>h </xsl:if>
		<xsl:if test="$min"><xsl:value-of select="number(substring($min, string-length($min) - 1, 2))"/>min </xsl:if>
		<xsl:if test="$sec != '00'"><xsl:value-of select="number($sec)"/>s</xsl:if>
	</xsl:variable>

	<time class="duration" datetime="{$datetime}"><xsl:value-of select="$datetime"/></time>
</xsl:template>


<xsl:template name="string-join">
	<xsl:param name="node"/>
	<xsl:param name="str-glue"/>

	<xsl:for-each select="$node">
		<xsl:value-of select="text()"/>
		<xsl:if test="position() != last()">
			<xsl:value-of select="$str-glue"/>
		</xsl:if>
	</xsl:for-each>
</xsl:template>



<xsl:template name="coordinates-join">
	<xsl:param name="coordinates"/>

	<xsl:call-template name="string-join">
		<xsl:with-param name="node" select="$coordinates/*"/>
		<xsl:with-param name="str-glue" select="':'"/>
	</xsl:call-template>
</xsl:template>


<xsl:template name="render-planet">
	<xsl:param name="planet"/>

<!-- 	[<xsl:value-of select="$planet/@id"/>] -->
	<xsl:call-template name="coordinates-join">
		<xsl:with-param name="coordinates" select="$planet/coordinates"/>
	</xsl:call-template>
	<xsl:if test="boolean($planet/name/text())">
		- <xsl:value-of select="$planet/name"/>
	</xsl:if>
</xsl:template>


<xsl:template name="header">
	<header>
		<hgroup>
			<h1 id="logo">Sunwars</h1>
			<h2>Jeu multi-joueurs par navigateur</h2>
		</hgroup>
		<nav>
			<ul>
				<li><a href="../" title="déconnection">Se déconnecter</a></li>
			</ul>
		</nav>
	</header>
</xsl:template>


<xsl:template name="footer">
	<footer>
		<address>
			<a href="?page=bug" title="rapport de bug">Rapport de bug</a>
			<!--contacter <a>l'administrateur</a>-->
		</address>
	</footer>
</xsl:template>


<xsl:template name="select-planet">
	<form method="get" action="{$page}">
		<xsl:call-template name="hidden-request-keep"/>
		<label for="select_planet">Planète :</label>
		<select id="select_planet" name="select_planet">
			<xsl:variable name="planet-current" select="player/@planet-current"/>

			<xsl:for-each select="planet">
				<option value="{@id}">
					<xsl:if test="$planet-current = @id">
						<xsl:attribute name="selected">selected</xsl:attribute>
					</xsl:if>
					<xsl:call-template name="render-planet">
						<xsl:with-param name="planet" select="."/>
					</xsl:call-template>
				</option>
			</xsl:for-each>
		</select>
		<input type="submit" value="Choisir" />
	</form>
</xsl:template>


<xsl:template name="navigation">
	<nav role="navigation" id="menu-principal">
		<ul>
			<li><a href="?page=structure" title="gestion de la planète">Gestion de la planète</a></li>
			<li><a href="?page=move" title="déplacement de vaisseaux">Mouvement</a></li>
			<li><a href="?page=galaxy" title="planètes du jeu">Voir les planètes</a></li>
			<li><a href="?page=message" title="voir ses message">Message</a></li>
			<li><a href="?page=options" title="configuration des options">Options</a></li>
		</ul>

		<xsl:call-template name="select-planet"/>
	</nav>
</xsl:template>


<xsl:template name="value-of-id">
	<xsl:param name="names"/>
	<xsl:param name="id"/>

	<xsl:value-of select="$names[@id=$id]"/>
</xsl:template>


<xsl:template match="resources">
	<xsl:if test="count(resource) != 0">
		<xsl:variable name="names-resource" select="$names/resources/resource"/>

		<table>
			<caption>Ressources</caption>
			<thead>
				<tr>
					<td></td>
					<xsl:for-each select="resource">
						<th>
							<xsl:call-template name="value-of-id">
								<xsl:with-param name="names" select="$names-resource"/>
								<xsl:with-param name="id" select="@group"/>
							</xsl:call-template>
						</th>
					</xsl:for-each>
				</tr>
			</thead>
			<tbody>
				<tr>
					<th>Quantité</th>
					<xsl:for-each select="resource">
						<td><xsl:value-of select="sum(quantity)"/></td>
					</xsl:for-each>
				</tr>
				<tr>
					<th>Par heure</th>
					<xsl:for-each select="resource">
						<td><xsl:value-of select="sum(quantity/@per-hour)"/></td>
					</xsl:for-each>
				</tr>
			</tbody>
		</table>
	</xsl:if>
</xsl:template>


<xsl:template match="debug">
	<section class="debug" role="debug">
		<xsl:for-each select="node()">
			<article>
				<xsl:call-template name="xparse-tag">
					<xsl:with-param name="root" select="."/>
				</xsl:call-template>
			</article>
		</xsl:for-each>
	</section>
</xsl:template>


<xsl:template match="/sun-wars">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
		<head>
			<meta charset="UTF-8" />
			<title>
				<xsl:if test="page/title">
					<xsl:value-of select="page/title"/>
				</xsl:if>
				 - Sun-Wars univers <xsl:value-of select="@universe"/>
			</title>

			<!--<meta name="description" content="">
			<meta name="keywords" content="">
			<meta name="author" content="">-->

			<xsl:for-each select="page/styles/css">
				<link rel="stylesheet" type="text/css" href="css/{text()}" media="screen" charset="utf-8" />
			</xsl:for-each>

			<link rel="shortcut icon" href="favicon.ico" />
		</head>
		<body role="document">
			<xsl:call-template name="header"/>
			<xsl:call-template name="navigation"/>

			<xsl:apply-templates select="debug"/>

			<xsl:variable name="id-planet-current" select="player/@planet-current"/>
			<xsl:apply-templates select="planet[@id=$id-planet-current]/resources"/>

			<section class="content" role="main">
				<xsl:call-template name="content">
					<xsl:with-param name="id-planet-current" select="$id-planet-current"/>
				</xsl:call-template>
			</section>

			<xsl:call-template name="footer"/>

			<xsl:for-each select="page/scripts/js">
				<script type="application/javascript" src="js/{text()}" />
			</xsl:for-each>
		</body>
	</html>
</xsl:template>


</xsl:stylesheet>